import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Codec {
    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        if (root == null) return "#";
        String s = root.val + "," + serialize(root.left) + "," + serialize(root.right);
        return s;
    }

    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        String[] ss = data.split(",");
        return process(new LinkedList<>(Arrays.asList(ss)));
    }

    public TreeNode process(List<String> list) {
        if (list.get(0).equals("#")) {
            list.remove(0);
            return null;
        }
        TreeNode node = new TreeNode(Integer.parseInt(list.get(0)));
        list.remove(0);
        node.left = process(list);
        node.right = process(list);
        return node;
    }
}
